package com.kk.service.impl;

import com.kk.Utils.ExcelToHomeworkUtil;
import com.kk.entity.Homework;
import com.kk.dao.HomeworkDao;
import com.kk.service.HomeworkService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * (Homework)表服务实现类
 *
 * @author makejava
 * @since 2025-04-30 18:40:06
 */
@Service("homeworkService")
public class HomeworkServiceImpl implements HomeworkService {
    @Resource
    private HomeworkDao homeworkDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Homework queryById(Integer id) {
        return this.homeworkDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param homework    筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<Homework> queryByPage(Homework homework, PageRequest pageRequest) {
        long total = this.homeworkDao.count(homework);
        return new PageImpl<>(this.homeworkDao.queryAllByLimit(homework, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param homework 实例对象
     * @return 实例对象
     */
    @Override
    public Homework insert(Homework homework) {
        this.homeworkDao.insert(homework);
        return homework;
    }

    /**
     * 修改数据
     *
     * @param homework 实例对象
     * @return 实例对象
     */
    @Override
    public Homework update(Homework homework) {
        this.homeworkDao.update(homework);
        return this.queryById(homework.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.homeworkDao.deleteById(id) > 0;
    }

    @Override
    public int addHomework(MultipartFile file) {
        List<Homework> homework = ExcelToHomeworkUtil.readExcel(file);
        return homeworkDao.insertBatch(homework);
    }
}
